﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZG_member.Model;
using Maticsoft.DBUtility;
using System.Data.SqlClient;
using System.Data;

namespace ZG_member.DAL
{
    public class taocanliushuiDAL
    {
        public int Add(ZG_member.Model.taocanliushui model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into taocanliushui(");
            strSql.Append("memberid,goodid,goodcount,time)");
            strSql.Append(" values (");
            strSql.Append("@memberid,@goodid,@goodcount,@time)");
            SqlParameter[] parameters = {
					new SqlParameter("@memberid", SqlDbType.NVarChar,50),
					new SqlParameter("@goodid", SqlDbType.NVarChar,50),
                    new SqlParameter("@goodcount", SqlDbType.NVarChar,50),
					new SqlParameter("@time", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.Memberid;
            parameters[1].Value = model.Goodid;
            parameters[2].Value = model.Goodcount;
            parameters[3].Value = model.Time;



            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


        public DataSet GetAllPage(int pageSize, int pageNum,string strWhere)
        {
            string sql = "";
            //select top(12) * from taocanliushui
            //where id not in (select top((1-1)*12) id from taocanliushui) 
            sql += "select top("+pageSize+") * from taocanliushui";
            if (strWhere == "")
            {
                sql += " where id not in (select top((" + pageNum + "-1)*" + pageSize + ") id from taocanliushui)";
            }
            else
            {
                sql += " where id not in (select top((" + pageNum + "-1)*" + pageSize + ") id from taocanliushui where "+strWhere+") and  "+ strWhere +"";
            }
            return DbHelperSQL.Query(sql);
        }

        public DataSet GetAll()
        {
            string sql = "select * from taocanliushui";
            return DbHelperSQL.Query(sql);
        }

        public int updatememberid(string newid, string oldid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update taocanliushui set ");
            strSql.Append("memberid =@oldid ");
            strSql.Append(" where memberid = @memberid");

            SqlParameter[] parameters = {
					new SqlParameter("@oldid",SqlDbType.NVarChar,4000),
					new SqlParameter("@memberid",  SqlDbType.NVarChar,50)};
            parameters[0].Value = oldid;
            parameters[1].Value = newid;


            object obj = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        public bool Delete(string memberid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from taocanliushui ");
            strSql.Append(" where memberid=@memberid");
            SqlParameter[] parameters = {
					new SqlParameter("@memberid", SqlDbType.NVarChar,50)
};
            parameters[0].Value = memberid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


    }
}
